package com.tckdq.tcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.tckdq.tcc.domain.Blacklist;
import com.tckdq.tcc.exception.GlobalException;
import com.tckdq.tcc.mapper.BlacklistMapper;
import com.tckdq.tcc.service.IBlacklistService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-08-17
 */
@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements IBlacklistService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private BlacklistMapper blacklistMapper;

    private static final String BLACK_LIST = "blacklist";

    @Override
    public void saveBlackList(Blacklist blacklist) {
        if (redisTemplate.hasKey(BLACK_LIST+blacklist.getPhone())) throw new GlobalException("请不要重复添加黑名单",null);
        /*Wrapper wrapper = new EntityWrapper<Blacklist>();
        wrapper.eq("phone",blacklist.getPhone());
        List<Blacklist> list = blacklistMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            throw new GlobalException("请不要重复添加黑名单",null);
        }*/
        blacklist.setSendtime(new Date());
        blacklistMapper.insert(blacklist);
        redisTemplate.opsForValue().set(BLACK_LIST+blacklist.getPhone(),blacklist);
    }

    @Override
    public boolean insert(Blacklist entity) {

        redisTemplate.opsForValue().set(BLACK_LIST+entity.getPhone(),entity);
        try {
            return super.insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("保存黑名单数据库失败",null);
        }
    }

    //删除之前redis的
    @Override
    public boolean update(Blacklist entity, Wrapper<Blacklist> wrapper) {
        redisTemplate.delete(BLACK_LIST+entity.getPhone());
        redisTemplate.opsForValue().set(BLACK_LIST+entity.getPhone(),entity);

        try {
            return super.update(entity, wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException("更新黑名单数据库失败",null);
        }
    }

    @Override
    public boolean delete(Wrapper<Blacklist> wrapper) {
        List<Blacklist> blacklists = blacklistMapper.selectList(wrapper);
        for (Blacklist blacklist : blacklists) {
            redisTemplate.delete(BLACK_LIST+blacklist.getPhone());
        }
        return super.delete(wrapper);
    }

    @Override
    public boolean deleteBatchIds(Collection<? extends Serializable> idList) {
        List<Blacklist> blacklists = blacklistMapper.selectBatchIds(idList);
        for (Blacklist blacklist : blacklists) {
            redisTemplate.delete(BLACK_LIST+blacklist.getPhone());
        }

        return super.deleteBatchIds(idList);
    }
}
